layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'laydate', 'upload'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var upload = layui.upload;

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimitpd',
        value: new Date().getFullYear(),
        type: 'year',
    });
    var Notice = {
        tableId: "noticeTablepd"    //表格id
    };

    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxda/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#dmpd").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');
    /**
     * 导入门店盘点Excel
     */
    var loadIndex;
    upload.render({
        elem: '#btnMdpdExcel1'
        , url: '/mdpd/importMdpd'
        , accept: "file"
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        ,before: function(obj){//参数
                loadIndex = layer.load('正在导入...',2, { shade: [0.15, '#ccc'] });
            }
        , done: function (res) {
            layer.close(loadIndex);
            if (res.state == 0) {
                alert("导入失败！");
            }
            if (res == true) {
                Feng.success("导入成功！");
                table.reload(Notice.tableId);
                $("#btnYhyyeExcel").unbind("click")
            }
        }
    });

    // 导入excel
    $('#btnMdpdExcel').click(function (event) {
        var dm = $("#dmpd").val();
        if (dm == ''){
            layer.msg('请选择店名！',{icon: 2, time: 1000})
        } else {
            $('#btnMdpdExcel1').click()
        }
    });
    // 搜索按钮点击事件
    $('#btnSearchpd').click(function () {
        Notice.search();
    });

    // 导出excel
    $('#btnExppd').click(function () {
        Notice.exportExcel();
    });

    /**
     * 导出excel按钮
     */
    Notice.exportExcel = function () {
        //获取导出Excel的数据
        // var ajax = new $ax(Feng.ctxPath + "/mdpd/listExportMdpd");
        // var dm = $("#dmpd").val();
        // var timeLimit = $("#timeLimitpd").val();
        // ajax.set("timeLimit", timeLimit);
        // ajax.set("dm", dm);
        // var result = ajax.start();
        // //身份证银行卡导出excel科学计数法问题
        // var lists = result;
        // table.exportFile(tableResult.config.id, lists, 'xls');

        var dm = $("#dmpd").val();
        var timeLimit = $("#timeLimitpd").val();
        //获取导出Excel的数据
        var url = Feng.ctxPath + "/ckpx/expExcel?dm=" + dm+"&timeLimit="+timeLimit;
        location.href = url;
    };

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            //{align: 'center', type: "numbers", title: '序号'},
            {field: 'dm', align: "center", sort: false, title: '店名',width: 200},
            {field: 'pxlb', align: "center", sort: false, title: '品项类别',width: 100},
            {field: 'pxbh', align: "center", sort: false, title: '品项编号', width: 100,},
            {field: 'pxmc', align: "center", sort: false, title: '品项名称', width: 150},
            {field: 'bzxx', align: "center", sort: false, title: '包装信息', width: 90},
            {field: 'pxgg', align: "center", sort: false, title: '品项规格', width: 120},
            {field: 'zcsl', align: "center", sort: false, title: '账存数量1', width: 100},
            {field: 'dw', align: "center", sort: false, title: '单位1'},
            {field: 'zcsl_two', align: "center", sort: false, title: '账存数量2',},
            {field: 'dw_two', align: "center", sort: false, title: '单位2',},
            {field: 'spsl', align: "center", sort: false, title: '实盘数量1',width: 100},
            {field: 'dw_three', align: "center", sort: false, title: '单位1'},
            {field: 'spsl_two', align: "center", sort: false, title: '实盘数量2',width: 100},
            {field: 'dw_four', align: "center", sort: false, title: '单位2',width: 100},
            {field: 'zcje', align: "center", sort: false, title: '账存金额',width: 100},
            {field: 'spje', align: "center", sort: false, title: '实盘金额',width: 100},
            {field: 'cysl', align: "center", sort: false, title: '差异数量',width: 100},
            {field: 'dw_five', align: "center", sort: false, title: '单位',width: 100},
            {field: 'ykje', align: "center", sort: false, title: '盈亏金额',width: 100},
            {field: 'fzdw', align: "center", sort: false, title: '辅助单位',width: 100},
            {field: 'fzdwzcsl', align: "center", sort: false, title: '辅助单位账存数量',width: 150},
            {field: 'fzdwspsl', align: "center", sort: false, title: '辅助单位实盘数量',width: 150},
            {field: 'fzdwcysl', align: "center", sort: false, title: '辅助单位差异数量',width: 150},
            {field: 'kwh', align: "center", sort: false, title: '库位号',width: 100},
            {field: 'pch', align: "center", sort: false, title: '批次号',width: 100},
            {field: 'bz', align: "center", sort: false, title: '备注',width: 100},
            //{align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 150}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/mdpd/list',
        page: true,
        height: "full-120",
        cellMinWidth: 100,
        limit: 20,
        totalRow: true,	//开启合计行
        cols: Notice.initColumn(),
    });


    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['dm'] = $("#dmpd").val();
        queryData['timeLimit'] = $("#timeLimitpd").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };



});